<template>
  <div class="comment-reply">
    <van-nav-bar :title="comment.reply_count>0 ? `${comment.reply_count}条回复` : '暂无回复'">
      <van-icon @click="$emit('close')" slot="left" name="cross" />
    </van-nav-bar>
    <div class="scroll-wrap">
      <!-- 当前评论项 -->
      <CommentItem :comment="comment"></CommentItem>
      <!-- 当前评论项 -->
      <!-- 评论的回复列表 -->
      <van-cell title="全部回复">
      </van-cell>
      <!-- 评论的回复列表 -->
      <CommentList @onload-success="updateComment" :list2="commentList" :source="comment.com_id" type="c"></CommentList>
    </div>
    <!-- 发布回复评论 -->
    <div class="reply-bottom">
      <van-button class="write-btn" size="small" round @click="isPostShow = true">写评论</van-button>
    </div>
    <van-popup v-model="isPostShow" position="bottom">
      <CommentPost @post-success="onPostSuccess" :target="comment.com_id"></CommentPost>
    </van-popup>
  </div>
</template>

<script>
import CommentItem from './comment-item.vue'
import CommentList from './comment-list.vue'
import CommentPost from './comment-post.vue'
export default {
  name: 'CommentReply',
  components: {
    CommentItem,
    CommentList,
    CommentPost
  },
  props: {
    comment: {
      type: Object,
      required: true
    }
  },
  data() {
    return {
      isPostShow: false,
      commentList: [] // 评论的回复列表
    }
  },
  computed: {},
  watch: {},
  created() {},
  mounted() {},
  methods: {
    onPostSuccess(data) {
      // console.log(data)
      // 让评论数量加一
      this.comment.reply_count++
      // 关闭弹层
      this.isPostShow = false
      // 让其渲染到页面的顶部
      this.commentList.unshift(data.new_obj)
    },
    updateComment(data) {
      this.commentList = [...this.commentList, ...data.results]
    }
  }
}
</script>

<style scoped lang="less">
.comment-reply {
  /deep/.van-nav-bar__title {
    color: #323233 !important;
  }
  .scroll-wrap {
    position: fixed;
    top: 92px;
    bottom: 88px;
    left: 0;
    right: 0;
    overflow-y: auto;
  }
  .reply-bottom {
    position: fixed;
    bottom: 0;
    left: 0;
    right: 0;
    height: 88px;
    display: flex;
    align-items: center;
    justify-content: center;
    background-color: #fff;
    border-top: 1px solid #d8d8d8;
    .write-btn {
      width: 60%;
    }
  }
}
</style>
